package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.KeywordMapper;
import com.banmao.mall.mapper.SearchHistoryMapper;
import com.banmao.mall.model.pojo.Keyword;
import com.banmao.mall.model.pojo.SearchHistory;
import com.banmao.mall.model.vo.client.search.SearchIndexVO;
import com.banmao.mall.service.client.SearchService;
import com.banmao.mall.util.CustomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public SearchIndexVO index() {
        /**
         * 1.从数据库查询默认的关键词和热词
         * 2.获取用户id
         * 3.根据用户id从数据库获取历史搜索词
         * 4.赋值给VO对象，返回。
         *
         * 注意：从数据库查询数据的语句应该catch，因为若是没有数据库为空，会返回异常。
         *       所以当捕获到异常应该赋值null。
         */
        SearchIndexVO searchIndexVO = new SearchIndexVO();
        //1、2
        Keyword record1 = new Keyword();
        record1.setIsDefault(true);
        List<Keyword> defaultKeywordList = keywordMapper.selectByNeed(record1);
        Keyword defaultKeyword = defaultKeywordList.get(0);
        Keyword record2 = new Keyword();
        record2.setIsHot(true);
        List<Keyword> hotKeywordList = keywordMapper.selectByNeed(record2);
        searchIndexVO.setDefaultKeyword(defaultKeyword);
        searchIndexVO.setHotKeywordList(hotKeywordList);
        //3
        List<SearchHistory> historyKeywordList;
        Integer userId = null;
        //判断用户是否登录，若未登录会抛出异常，不作处理，将历史记录设为null即可
        try {
            userId = CustomUtils.judgeUserLogin();
        } catch (IllegalAccessException e) {
        }
        if (userId == null) {
            historyKeywordList = null;
        } else {
            //若能获取到用户id，从数据库查询。
            SearchHistory record3 = new SearchHistory();
            record3.setUserId(userId);
            historyKeywordList = searchHistoryMapper.selectByNeed(record3);
        }
        searchIndexVO.setHistoryKeywordList(historyKeywordList);
        return searchIndexVO;
    }

    @Override
    public List<String> helper(String keyword) {
        Keyword record = new Keyword();
        record.setKeyword(keyword);
        record.setIsHot(true);
        List<Keyword> keywordList = keywordMapper.selectByNeed(record);
        List<String> stringList = new ArrayList<>();
        for (Keyword keyword1 : keywordList) {
            stringList.add(keyword1.getKeyword());
        }
        return stringList;
    }

    @Override
    public int clearhistory() {
        Integer userId = null;
        //因为若未登录用户无法点击清除记录按钮，故发出该请求则必定已登录
        try {
            userId = CustomUtils.judgeUserLogin();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setUserId(userId);
        searchHistory.setDeleted(true);
        int affectRows = searchHistoryMapper.updateDeleted(searchHistory);
        return 0;
    }


}
